<?php

class AccountController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		$this->commonAction();	
	}
	
	
	public function indexAction(){
		try {
			// echo 'start<br />';
			//$sql="select  *  from version where 1 order by addtime desc limit $offset,$count";
			//$result = $this->_dbAdapter->fetchAll ($sql);	
					
			//$sql="select  groupid,groupname from  groups ";
			//$result = $this->_dbAdapter->fetchAll ($sql);	
		 	//$this->view->result= $result; 
			//多少个家长
			 
			$sql="select pid from parents  where username+0<100000 or username+0>999999  order by pid Asc  limit 0,1";
			$pnum = $this->_dbAdapter->fetchCol ($sql);
			// print_r($pnum);
			  echo $pnum[0].'<br>';
		      for ($i=0; $i<=count($pnum); $i++)
			 	{ 
			     $key=rand(100000,999999);
				//$key=100000;	
				$password=rand(100000,999999);
				$sql="select  *  from  parents where username='".$key."'  ";
				 
				$pusername = $this->_dbAdapter->fetchAll ($sql);
				 
				if(!empty($pusername)){ 
				  $key=rand(100000,999999);	
				} else if(empty($pusername)){
				 $sql='UPDATE parents SET  username = "'.$key.'",password="'.$this->authcode($password,'','mactop').'"  WHERE pid ="'.$pnum[$i].'" ';  
				 $result = $this->_dbAdapter->query ($sql); 		 
					} 
			  	}
				 
				
			 /*
				$sql='select pid from parents where password="" limit 100';
				$result=$this->_dbAdapter->fetchCol($sql);
				foreach($result as $key => $value){
					$randkey=rand(100000,999999);	
					$randkey=$this->authcode($randkey,'','mactop');
			 		$result = $this->_dbAdapter->update ('parents',array('password'=>$randkey),'pid ='.$value); 	
				}
				
				*/
				//print_r($result);	
				$randkey=rand(100000,999999);	
			  //$this->_redirect ( '/account/index/key/'.$randkey);
			  //$this->feedback ( '', 'note', '/account/index', 'warning',0 );
			   echo $randkey;
			   echo '<script language="javascript" type="text/javascript"> 
						location.href="http://base.yunxiaoche.com/account/index/key/'.$randkey.'"; 
					</script>';
					
					exit;
				//$this->feedback ( '更新成功', '注意', '/'.$this->controller.'/index', 'warning',0 );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}


	public function bindcodeAction(){
		try { 
		   
              $sql="select * from groups  where groupid='3' ";
              $result=$this->_dbAdapter->fetchAssoc($sql);
             foreach($result as $key => $value){ 
/*			  	//连接运营库
				$dbArray=array();
				$dbArray['host']=$value['dbhost'];
				$dbArray['username']=$value['dbusername'];
				$dbArray['password']=$this->authcode($value['dbpassword'], 'DECODE', 'mactop') ;
				$dbArray['dbname']=$value['dbname'];
				
				$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
				Zend_Registry::set('dbAdapter', $dbAdapter);
	
				$dbAdapter=Zend_Registry::get('dbAdapter');
				$dbAdapter->query ( 'set names utf8');	 
*/				 
				//  $string;  
				  
				$sql="select studentid from  student where bindcode='' limit 0,100" ;
				$studentid= $dbAdapter->fetchCol ($sql);  
				 	
                 
			  
				foreach($studentid as $key =>$value){
				  
					  //echo $string;  exit;
					$data=array('bindcode'=>$this->randcode()) ;
			     	$studentid= $this->_dbAdapter->update ('student',$data,'studentid='.$value); 
				  
				}
				
			 }
			   $randkey=rand(100000,999999);	
	           echo '<script language="javascript" type="text/javascript">  
						 location.href="http://base.yunxiaoche.com/account/bindcode/key/'.$randkey.'"; 
					</script>';
					
					exit;   
				
						  
   /*
             $dir0=sprintf("%03d", '1'); 
		     //$num=substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 1); 
			$string='';
			$str1 = "0123456789abcdefghijklmnopqrstuvwxyz";   //输出字符集    
			$n = 1;   //输出串长度    
			$len = strlen($str1)-1;  
			 
			for($j=0 ; $j<5 ; $j++){  
			   $str1=str_shuffle($str1);
				for($i=0 ; $i<$n; $i++){  
					$s .= $str1[rand(0,$len)];    
				}   
			 } 
			$string.= $s;   
			echo $string;  */
		
			



	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}



               public function randcode($groupid=3,$len=5){
					
					$string = sprintf("%03d", $groupid); 
					//$num=substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 1); 
					
					$str1 = "0123456789abcdefghijklmnopqrstuvwxyz";   //输出字符集    
					
					$strlen = strlen($str1)-1;  
					for($j=0 ; $j<$len ; $j++){  
							$s = substr($str1,rand(0,$strlen),1);  
							 $string .= $s;   
					}   
					 
				  return $string;
				
               }

	public function accountAction(){
		try {
			//查询所有的学生的信息
		 	 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function parentsAction(){
		try {
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			$sql="select  *  from version where version_type=1 order by addtime desc limit $offset,$count";
			$result = $this->_dbAdapter->fetchAll ($sql);			
			
			$this->view->result= $result; 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function addAction(){
		try {
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function opaddAction(){
		try {
			if (! $this->isPost ()) {
					throw new Exception ( 'parameter_error' );
			}
		 	//获取post提交的值
			$p =$this->_request->getPost ();
			//	print_r($p);exit;
			
			if($p['version_no']=="" || $p['version_no']==null){
				throw new Exception ( '版本号不能为空！' );
			}
			if($p['version_url']=="" || $p['version_url']==null){
				/*
				$ext= substr(strrchr($_FILES["file"]["name"],'.'),1);//文件后缀
				if($_FILES["file"]["size"]<=0){
					throw new Exception ( '请选择文件！' );
				}
				if($ext!='apk'){
					throw new Exception ( '文件格式错误，请重新选择文件！' );
				}
				
				$allfile=substr(PROJECT_ROOT,0,-6);
				$file_size=$_FILES["file"]["size"]/1024000;//文件大小
				$file_size=substr($file_size,0,4);
				$file_name=$_FILES["file"]["name"];//文件名称				
				if($p['version_type']==1){
					$savePath=$allfile.'www/parents/';
					//$file_url='http://www.yunxiaoche.com/parents/';
				}else{
					$savePath=$allfile.'www/teacher/';
					//$file_url='http://www.yunxiaoche.com/teacher/';
				}
				move_uploaded_file($_FILES['file']['tmp_name'],$savePath.'School_Bus_P'.$p['version_no'].'.apk');
				if($p['version_type']==1){
					$file_name='http://www.yunxiaoche.com/parents/School_Bus_P'.$p['version_no'].'.apk';
				}else{
					$file_name='http://www.yunxiaoche.com/teacher/School_Bus_T'.$p['version_no'].'.apk';
				}
				$data=array(
					'version_no'=>$p['version_no'],
					'version_explain'=>$p['version_explain'],
					'version_url'=>$file_name,
					'version_type'=>$p['version_type'],
					//'file_size'=>$file_size,
					'addtime'=>time(),
				);*/
			}else{
				$file_name=$p['version_url'];
				$isfile=$this->check_remote_file_exists($file_name);
				
				if(!$isfile){
					throw new Exception ( '文件不存，请先上传文件' );
				}
				
				$data=array(
					'version_no'=>$p['version_no'],
					'version_explain'=>$p['version_explain'],
					'version_url'=>$file_name,
					'version_type'=>$p['version_type'],
					//'file_size'=>$p['version_size'],
					'addtime'=>time(),
				);
			}
				
			$result = $this->_dbAdapter->insert ('version',$data);
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller, 'tip', 3 );
			} else {
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//增加版本上传至云
	public function opaddyunAction(){
		try {
			if (! $this->isPost ()) {
					throw new Exception ( 'parameter_error' );
			}
		 	//获取post提交的值
			$p =$this->_request->getPost ();
			//	print_r($p);exit;
			
			if($p['version_no']=="" || $p['version_no']==null){
				throw new Exception ( '版本号不能为空！' );
			}
			if($_FILES["file_url"]["size"]<=0){
				throw new Exception ( '请选择文件！' );
			}
			$ext= substr(strrchr($_FILES["file_url"]["name"],'.'),1);//文件后缀
			if($ext!='apk'){
				throw new Exception ( '文件格式错误，请重新选择文件！' );
			}
			$urls=PROJECT_ROOT .'/public/apk/';
			if($p['version_type']==1){
				$file_name='School_Bus_P';
			}else{
				$file_name='School_Bus_T';
			}
			$yunpath=$urls.$file_name.$p['version_no'].'.apk';
			//上传文件
			move_uploaded_file($_FILES['file_url']['tmp_name'],$urls.$file_name.$p['version_no'].'.apk');
			if($p['version_type']==1){
				$file_url='http://cloud.yunxiaoche.com/apk/'.$file_name.$p['version_no'].'.apk';
			}else{
				$file_url='http://cloud.yunxiaoche.com/apk/'.$file_name.$p['version_no'].'.apk';
			}
			$res=Azure_Storage::defaultImg('apk',$yunpath,$file_name.$p['version_no'].'.apk');//上传云
			if($res){
				//删除本地图片
				unlink($yunpath);
			}else{
				throw new Exception ('上传失败：'.$res);
			}
			$data=array(
				'version_no'=>$p['version_no'],
				'version_explain'=>$p['version_explain'],//版本说明
				'version_url'=>$file_url,//绝对地址云地址
				'version_type'=>$p['version_type'],//类别1、家长版；2、教师表
				//'file_size'=>$p['version_size'],
				'addtime'=>time(),
			);
			$result = $this->_dbAdapter->insert ('version',$data);
			if ($result) {
				//echo '1';exit;
				$this->feedback ( '上传成功地址为：'.$res, '上传成功', '/'.$this->controller, 'tip', 3 );
			} else {
				//echo '-1';exit;
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function deleteAction(){
		try {
			$vid = ( int ) $this->_request->getParam ( 'vid' );
			if(!$vid)
				throw new Exception ( 'parameter_error' );
			$data=array(
					//'foodinfo'=>$p['info'],
					'isdelete'=>1,
				);
			$result = $this->_dbAdapter->update ( 'version', $data,'versionid='.$vid );
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller, 'tip', 3 );
			} else {
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

